package com.xk._02算法篇._01sort.sort.cmp;

import com.xk._02算法篇._01sort.sort.Sort;
import com.xk.tools.Integers;

/**
 * @description: 冒泡排序
 * @author: xu
 * @date: 2022/10/1 2:14
 */
public class BubbleSort1<E extends Comparable<E>> extends Sort<E> {
    /**
     * 冒泡排序
     */
    private void bubbleSort1(){
        for (int end = array.length - 1; end > 0; end--) {
            for (int begin = 1; begin <= end; begin++) {
                if (cmp(begin, begin-1) < 0){
                    swap(begin, begin-1);
                }
            }
        }
    }

    @Override
    protected void sort() {
        bubbleSort1();
    }


    public static void main(String[] args) {
        Integer[] array1 = Integers.random(20, 1, 100);
        Integers.println(array1);
        Integer[] array2 = Integers.copy(array1);
        Integer[] array3 = Integers.copy(array1);

        Sort<Integer> bubbleSort1 = new BubbleSort1<>();
        bubbleSort1.sort(array1);

        /*Times.test("bubbleSort1", () -> new BubbleSort1().bubbleSort1(array1));
        Times.test("bubbleSort2", () -> new BubbleSort1().bubbleSort2(array2));
        Times.test("bubbleSort3", () -> new BubbleSort1().bubbleSort3(array3));

        System.out.println(Integers.isAscOrder(array1));
        System.out.println(Integers.isAscOrder(array2));
        System.out.println(Integers.isAscOrder(array3));*/
    }
}
